package cn.lyvust.first.manage.controller;


import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.dto.param.ZhangfirstTestParam;
import cn.lyvust.first.manage.entity.ZhangfirstTest;
import cn.lyvust.first.manage.service.ZhangfirstTestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张洪浩
 * @since 2021-02-09
 */
@Api(tags = "Zhangfirst")
@RestController
@Controller
@RequestMapping("/ZhangfirstTest")
public class ZhangfirstTestController {
    @Autowired
    private ZhangfirstTestService zhangfirstTestService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询信息")
    public ResponseResult list(@RequestBody ZhangfirstTestParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }

        return ResponseResult.e(ResponseCode.OK,zhangfirstTestService .list(wrapper));
    }


    @PostMapping(value = {"/add"})
    @ApiOperation(value = "添加信息")
    public ResponseResult add(@RequestBody ZhangfirstTestParam param){
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("添加失败,姓名不能为空!");
        }
        if (StringUtils.isEmpty(param.getPhone())) {
            throw RequestException.fail("添加失败,手机号码不能为空!");
        }
        ZhangfirstTest entity = new ZhangfirstTest();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK,zhangfirstTestService .save(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改信息")
    @SysLogs("修改")
    public ResponseResult update(@RequestBody ZhangfirstTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,ID不能为空!");
        }


        ZhangfirstTest entity = zhangfirstTestService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, zhangfirstTestService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除信息")
    @SysLogs("删除")
    public ResponseResult delete(@RequestBody ZhangfirstTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("请输入ID删除");
        }
        return ResponseResult.e(ResponseCode.OK, zhangfirstTestService.removeById(param.getId()));
    }
}
